package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"path"
	"strings"

	"gitee.com/Flearning/go_crawl/setting"
)

type (
	ReqCrawlInfo struct {
		Method        string            `json:"method,omitempty"`
		Url           string            `json:"url,omitempty"`
		MgoCollection string            `json:"mgoCollection,omitempty"`
		Header        map[string]string `json:"req_header,omitempty"`
		Prames        []CrawlPrames     `json:"prames,omitempty"`
	}
	CrawlPrames struct {
		Symbol string `json:"symbol,omitempty"`
		Page   int    `json:"page,omitempty"`
		Sort   string `json:"sort,omitempty"`
		Sleep  int64  `json:"sleep,omitempty"`
	}
)

func ReadCrawlSiteInfo(rb []byte) *ReqCrawlInfo {
	reqInfo := new(ReqCrawlInfo)
	json.Unmarshal(rb, reqInfo)
	return reqInfo
}

func main() {
	setting.NewConfigContext()
	setting.NewLogService()

	siteinfos := setting.Sites
	fmt.Println(siteinfos)

	pwd, _ := setting.WorkDir()
	fmt.Println(pwd)

	cfgPath := path.Join(pwd, "conf", siteinfos[0])
	ckFile := setting.IsFile(cfgPath)
	if ckFile {
		fmt.Println(cfgPath)
	} else {
		fmt.Errorf(" `%s` config file path not find.", cfgPath)
	}

	rb, err := ioutil.ReadFile(cfgPath)
	if err != nil {
		panic(err)
	}
	req := ReadCrawlSiteInfo(rb)

	referer := req.Header["Referer"]
	referer = strings.Replace(referer, "%symbol%", req.Prames[0].Symbol, 1)
	fmt.Println(referer)
}
